package com.fgchen.biz;

import com.fgchen.domain.Address;
import com.fgchen.domain.Person;
import com.fgchen.domain.study01.User;
import com.fgchen.domain.study02.Animal;
import com.fgchen.util.excel.ExcelConfig;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.stereotype.Service;

import java.io.InputStream;
import java.util.*;

/**
 * Created by Administrator on 2018-04-08.
 */
@Service
public class SummaryBiz {

    @Autowired
    private UserBiz userBiz;

    @Autowired
    private AnimalBiz animalBiz;

    @Autowired
    @Qualifier("personExcelConfig")
    private ExcelConfig excelConfig;

    @Autowired
    private ExcelBiz excelBiz;

    public Map<String, Collection<?>> summary() {
        Map<String, Collection<?>> map = new HashMap<>();
        map.put(User.class.getName(), this.userBiz.findAll());
        map.put(Animal.class.getName(), this.animalBiz.findAll());
        return map;
    }

    public InputStream exportExcel() throws Exception {
        List<Person> list = new ArrayList<>();
        Random random = new Random();
        for (int i=0; i<10; i++) {
            Person person = new Person();
            String name = UUID.randomUUID().toString().substring(0, 5);
            person.setName(name);
            person.setAge(random.nextInt(100));
            person.setEmail(String.format("%s@163.com", name));
            person.setAddress(new Address());
            person.getAddress().setProvince(name + "-province-" + i);
            person.getAddress().setCity(name + "-city-" + i);
            list.add(person);
        }
        return this.excelBiz.generateExcel(list, this.excelConfig);
    }

}
